/*
 * gios.c
 * General/Input/Output functions for uPL0c.
 *
 * Copyright 2024 dh33ex <dh33ex@riseup.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA or visit <http://www.gnu.org/licenses/>.
 *
 *
 */

#include "gios.h"
#include "generator.h"

#include <stdio.h>
#include <stddef.h>

/* definition of a global variable */
Compiler compiler = {0, 0, 0, ""};

/* a structure that stores a position in the source file */
static struct {
    long int line;
    long int character;
    long int old_character;
} pos = {1, 0};

/* file's */
static FILE *prog_file = NULL;
static FILE *output_file = NULL;

int read_from(char *filename) {
    prog_file = fopen(filename, "rb");
    if (NULL == prog_file) {
        return 0;
    } else {
        return 1;
    }
}


void write_bin(void) {
    unsigned int i;
    unsigned char bin;

    for (i = 0; i < MEMORY_SIZE; i++) {
        bin = code[i] & 0xFF;
        fwrite(&bin, 1, sizeof(bin), output_file);
        bin = (code[i] >> 8) & 0xFF;
        fwrite(&bin, 1, sizeof(bin), output_file);
        bin = (code[i] >> 16) & 0xFF;
        fwrite(&bin, 1, sizeof(bin), output_file);
        bin = (code[i] >> 24) & 0xFF;
        fwrite(&bin, 1, sizeof(bin), output_file);
    }
}


void write_txt(void) {
    for (int i = 0; i < MEMORY_SIZE; i++) {
        fprintf(output_file, "%d\n", code[i]);
    }
}


void write_code(void) {
    for (int i = 0; i < MEMORY_SIZE; i++) {
        if (0 != code[i]) {
            fprintf(output_file, "ROM[%d] = 32'h%x;\n", i, code[i]);
        }
    }
}


int write_to(char *filename) {
    output_file = fopen(filename, "wb");
    if (NULL == prog_file) {
        return 0;
    } else {
        return 1;
    }
}


void end_write(void) {
    if (NULL != prog_file) {
        fclose(prog_file);
    }

    if (NULL != output_file) {
        fclose(output_file);
    }
}


void print_error(char *msg) {
    /* print error message, current position and rise error flag */
    printf("E: %ld,%ld: %s\n", pos.line, pos.character, msg);
    compiler.error = 1;
}


void print_note(char *msg) {
    /* print note message, current position */
    printf("N: %ld,%ld: %s\n", pos.line, pos.character, msg);
}


int get_ch(void) {
    /* read char and increment the position variable accordingly */
    int ch = fgetc(prog_file);

    if (ch == '\n') {
        pos.line++;
        pos.old_character = pos.character;
        pos.character = 0;
        /* ch = get_ch(); */
    } else {
        pos.character++;
    }

    return ch;
}


void unget_ch(int ch) {
    /* put char back to the stream and decrease the position variable */
    ungetc(ch, prog_file);

    /* a workaround for the situation when the pos = (..., 0)*/
    if ( --pos.character < 0) {
        pos.line--;
        pos.character = pos.old_character;
    }
}
